﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Kaiser.Infrastructure.Controllers;
using Kaiser.Models.JsonModels;
using Kaiser.Models;
using System.Web.Script.Serialization;
using Kaiser.Extensions;
using Kaiser.Modules.CMS.Services;
using Kaiser.Modules.CMS.Models;
namespace Kaiser.Modules.CMS.Controllers
{
    public class CategoryController : KaiserBaseController
    {
        private ICMSManageService _ICMSManageService;
        public CategoryController(ICMSManageService cmsManageService)
        {
            _ICMSManageService = cmsManageService;
        }

        #region 分类管理
        public ActionResult CategoryList()
        {
            return View();
        }


        [HttpPost]
        public ActionResult CategoryList(CategorySearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = this._ICMSManageService.GetCategoryAll(search, pageinfo);
            return Content(result.ToJsonString());
        }



        public ActionResult GetCategoryTreeList(CategorySearch search, PagingInfo pageinfo, FormCollection form)
        {
            pageinfo.Size = 100000000;
            var result = this._ICMSManageService.CategoryTreeList(search, pageinfo);
            return Json(result);
        }



        public ActionResult AddCategory(string Code)
        {
            var model = new CategoryInput();
            CategoryInput parent = _ICMSManageService.GetCategory(Code);
            if (parent != null)
            {
                model.ParentId = parent.Id;
                ViewData["parentName"] = parent.CategoryName;
            }
            else
            {
                model.ParentId = 0;
                ViewData["parentName"] = "根";
            }

            return View("EditCategory", model);
        }

        public ActionResult EditCategory(string Code)
        {
            var model = new CategoryInput();
            if (!Code.IsNullOrEmpty())
            {
                model = _ICMSManageService.GetCategory(Code);
                CategoryInput parent = _ICMSManageService.GetCategory(model.ParentId.ToString());
                if (parent != null)
                    ViewData["parentName"] = parent.CategoryName;
                else
                    ViewData["parentName"] = "根";
            }
            return this.View(model);
        }

        [HttpPost]
        public ActionResult AddCategory(CategoryInput model, FormCollection form)
        {
            var result = new JsonReturnMessages();
            try
            {

                model.CategoryCode = Guid.NewGuid().ToString();
                model.CreateTime = DateTime.Now;
                model.SeoTitle = "";
                model.SeoH1 = "";
                model.SeoDes = "";
                model.CreateMan = "";
                model.CategoryUrl = "";
                model.CategoryImg = "";
                _ICMSManageService.InsertCategory(model);
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }




        [HttpPost]
        public ActionResult EditCategory(CategoryInput model, FormCollection form)
        {
            var result = new JsonReturnMessages();
            try
            {

                model = _ICMSManageService.GetCategory(model.Id.ToString());
                UpdateModel(model, form);
                model.CategoryDesc = Server.UrlDecode(model.CategoryDesc);
                _ICMSManageService.UpdateCategory(model);
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }



        /// <summary>
        /// 批量删除点餐
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelCategoryDetail(string ids)
        {

            JsonReturnMessages result = new JsonReturnMessages(); ;
            if (!ids.IsNullOrWhiteSpace())
            {
                result.success = _ICMSManageService.DeleteCategoryList(ids) > 0;
                if (result.success)
                {
                    result.msg = "删除成功";
                }
                else
                {
                    result.msg = "删除失败";
                }
            }
            else
            {
                result.msg = "传值失败，请告诉开发者解决";
            }
            JavaScriptSerializer json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }







        #endregion


        #region 内容管理

        [HttpPost]
        public ActionResult GetCategoryTree(CategorySearch search)
        {
            var pageinfo = new PagingInfo();
            pageinfo.Size = 10000;


            var result = _ICMSManageService.GetCategoryAll(new CategorySearch(), pageinfo);
            var tree = JsonTree.ConvertToJsonTree(result.TableResult, "CategoryName", "Id", "ParentId", "0");



            return Json(tree);
        }
        #endregion
    }
}
